﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using DemoMTWebService.WSSMSBrandname;

namespace DemoMTWebService
{
    /// <summary>
    /// Summary description for SMSMTService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class SMSMTService : System.Web.Services.WebService
    {
        [WebMethod]
        public string SendMT(string user, string pass, string sms, string senderName, string phone,
            bool isFlash, bool isUnicode)
        {
            string result = "";
            using (SqlConnection conn = Settings.AppSetting.GetConnection())
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"INSERT INTO SmsTestMessage
                                        VALUES(@Message, @SenderName, @Phone, @IsFlash, @IsUniCode)
                                        SELECT SCOPE_IDENTITY()";
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.Parameters.AddWithValue("@Message", sms ?? (object)DBNull.Value);
                    cmd.Parameters.AddWithValue("@SenderName", senderName ?? (object)DBNull.Value);
                    cmd.Parameters.AddWithValue("@Phone", phone ?? (object)DBNull.Value);
                    cmd.Parameters.AddWithValue("@IsFlash", isFlash);
                    cmd.Parameters.AddWithValue("@IsUniCode", isUnicode);
                    result = cmd.ExecuteScalar().ToString();
                }
            }
            return result;
        }

        [WebMethod]
        public string GetReport(int messageID)
        {
            string result = "";
            using (SqlConnection conn = Settings.AppSetting.GetConnection())
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT * FROM SmsTestMessage WHERE [ID] = @ID";
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.Parameters.AddWithValue("@ID", messageID);
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd != null && rd.HasRows) result = "SUCCESSFUL";
                        else result = "FAIL";
                    }
                }
            }
            return result;
        }

        [WebMethod]
        public string SendSMS(string userName, string password, string shortcode, string dest_addr, string msg )
        {
            using (SMSBrandname service = new SMSBrandname())
            {
                return service.SENDSMS(userName, password, shortcode, dest_addr, msg);
            }
        }

        [WebMethod]
        public string Status(string userName, string password, long ID, string code)
        {
            using (SMSBrandname service = new SMSBrandname())
            {
                return service.Status(userName, password, ID, code);
            }
        }
    }
}
